package com.cogent.dynamicform.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cogent.business.helper.BussinessDealHelper;
import com.cogent.business.helper.DynamicFormHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.domain.FormQueryDomain;
import com.cogent.dynamicform.domain.FormQueryDynTableDomain;
import com.cogent.dynamicform.domain.ListColumnDomain;
import com.cogent.dynamicform.facade.IDynamicFormFacade;
import com.cogent.dynamicform.facade.impl.DynamicFormFacadeImpl;
import com.cogent.validator.domain.ValidatorDomain;

public class DynamicFormBusUtil {
	public static String DYNAMICFORM_FILPATH = "imgsrc";
	public static String DYNAMICFORM_REALFILENAME = "_dynrealfilename";
	
	public static List getFormQueryList(FormDomain formDomain) {
		return getFormQueryList(formDomain, formDomain.getColumnMap());
	}
	
	public static List getFormQueryList(FormDomain formDomain, Map columnMap) {
		List retList = new ArrayList();
		String querycolumn = formDomain.getQuerycolumn();
		if(!StringUtil.isEmpty(querycolumn)) {			
			String[] temp = querycolumn.split(";");
			for(int i=0; i<temp.length; i++) {
				String key = temp[i];
				int querytype = 1;
				String columType = "";
				if(key.indexOf("|") > 0) {
					String[] temp2 = key.split("\\|");
					key = temp2[0];
					querytype = StringUtil.toInt(temp2[1], 1);
					if(temp2.length > 2)
						columType = temp2[2];
				}
				FormColumnDomain formColumnDomain = (FormColumnDomain)columnMap.get(key);
				if(StringUtil.isEmpty(columType))
					columType = formColumnDomain.getType();
				FormQueryDomain formQueryDomain = new FormQueryDomain(formColumnDomain.getName(), formColumnDomain.getCn()
														,columType, formColumnDomain.getTypecode(), querytype);
				formQueryDomain.setMapedFileld(formColumnDomain.getMapedfileld());
				formQueryDomain.setTablename(formColumnDomain.getTablename());
				formQueryDomain.setJs(formColumnDomain.getJs());
				formQueryDomain.setCalformula(formColumnDomain.getCalformula());
				formQueryDomain.setExpression(formColumnDomain.getExpression());
				formQueryDomain.setCountexpression(formColumnDomain.getCountexpression());
				retList.add(formQueryDomain);
			}
		}
		querycolumn = formDomain.getMorequerycolumn();
		if(!StringUtil.isEmpty(querycolumn)) {
			String[] temp = querycolumn.split(";");
			for(int i=0; i<temp.length; i++) {
				String key = temp[i];
				int querytype = 1;
				String columType = "";
				if(key.indexOf("|") > 0) {
					String[] temp2 = key.split("\\|");
					key = temp2[0];
					querytype = StringUtil.toInt(temp2[1], 1);
					if(temp2.length > 2)
						columType = temp2[2];
				}
				FormColumnDomain formColumnDomain = (FormColumnDomain)columnMap.get(key);
				if(StringUtil.isEmpty(columType))
					columType = formColumnDomain.getType();
				FormQueryDomain formQueryDomain = new FormQueryDomain(formColumnDomain.getName(), formColumnDomain.getCn()
														,columType, formColumnDomain.getTypecode(),	true, querytype);
				formQueryDomain.setMapedFileld(formColumnDomain.getMapedfileld());	
				formQueryDomain.setTablename(formColumnDomain.getTablename());
				formQueryDomain.setJs(formColumnDomain.getJs());
				formQueryDomain.setCalformula(formColumnDomain.getCalformula());
				formQueryDomain.setExpression(formColumnDomain.getExpression());
				formQueryDomain.setCountexpression(formColumnDomain.getCountexpression());
				retList.add(formQueryDomain);
			}
		}		
		
		return retList;
	}
	
	public static List getListColumn(FormDomain formDomain) {
		return getListColumn(formDomain, formDomain.getColumnMap());
	}
	
	public static List getListColumn(FormDomain formDomain, Map columnMap) {
		return getListColumn(formDomain.getListcolumn(), columnMap);
	}
	
	public static List getListColumn(String querycolumn, Map columnMap) {
		List retList = new ArrayList();
		if(!StringUtil.isEmpty(querycolumn)) {
			String[] temp = querycolumn.split(";");
			for(int i=0; i<temp.length; i++) {
				String key = temp[i];	
				String curwidth = "50";
				String type = "";
				if(key.indexOf("|") > 0) {
					String[] curtemp = key.split("\\|");
					key = curtemp[0];
					curwidth = curtemp[1];
					if(curtemp.length > 2)
						type = curtemp[2];
				}
				FormColumnDomain formColumnDomain = (FormColumnDomain)columnMap.get(key);
				if(StringUtil.isEmpty(type))
					type = formColumnDomain.getType();
				ListColumnDomain listColumnDomain = new ListColumnDomain(formColumnDomain.getName(), formColumnDomain.getCn()
														,type, formColumnDomain.getTypecode(), curwidth);
				listColumnDomain.setJs(formColumnDomain.getJs());
				listColumnDomain.setShowclass(formColumnDomain.getShowclass());
				
				retList.add(listColumnDomain);
			}
		}
		
		return retList;
	}
		
	public static List getValidatorList(List colunmList) {
		return getValidatorList(colunmList, ".");
	}
	
	public static List getValidatorList(List colunmList, String infix) {
		Iterator it = colunmList.iterator();
		List retList = new ArrayList();
		while(it.hasNext()) {
			FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
			if((formColumnDomain.getViewmodel() > 0) || (formColumnDomain.getShowtype() == 2) || (formColumnDomain.getValidator().size()== 0))
				continue;
			Iterator it2 = formColumnDomain.getValidator().iterator();
			while(it2.hasNext()) {
				ValidatorDomain validatorDomain = (ValidatorDomain)it2.next();
				validatorDomain.setInfix(infix);
				retList.add(validatorDomain);
			}
		}
		
		return retList;
	}

	public static void dealDynTableMap(List colunmlist, HashMap curmMap, HashMap keyparMap, String prefix, String infix, String formmapkey) {
		int num = 0;
		List retList = new ArrayList();
		IDynamicFormFacade dynamicformfacade = new DynamicFormFacadeImpl();
		while(true) {
			if(!curmMap.containsKey(prefix + num + "]" + infix + "id"))
				break;
			HashMap curdata = new HashMap();
			curdata.put("id", MapUtil.getStringValue(curmMap, prefix + num + "]" + infix + "id"));
			Iterator it = colunmlist.iterator();
			while(it.hasNext()) {
				FormColumnDomain formColumnDomain = (FormColumnDomain)it.next();
				if((formColumnDomain.getViewmodel() > 0) || (formColumnDomain.getShowtype() == 2))
					continue;
				curdata.put(formColumnDomain.getName(), MapUtil.getStringValue(curmMap, prefix + num + "]" + infix + "" + formColumnDomain.getName()));
				dynamicformfacade.dealFormValue(formColumnDomain, curdata);
			}
			MapUtil.copyMapValue(curdata, keyparMap);
			retList.add(curdata);
			num++;
		}
		
		curmMap.put(formmapkey, retList);
	}
	
	public static void dealDynTableMap(List colunmlist, HashMap curmMap) {
		dealDynTableMap(colunmlist, curmMap, new HashMap(), "dyntable[", ".", DynamicFormHelper.DYNTABLE_FORM_KEY);
	}
	
	public static void dealQueryDynTableMap(List colunmlist, HashMap curmMap) {
		dealDynTableMap(colunmlist, curmMap, new HashMap(), "querydyntable[", "_", DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY);
	}
	
	public static void dealDynTableMap(HashMap formmap, String prefix, String fields) {
		String[] temps = fields.split(",");
		List retList = new ArrayList();
		int i = 0;
		while(true) {
			if(!formmap.containsKey(prefix + i))
				break;
			HashMap curMap = new HashMap();
			for(int j=0; j<temps.length; j++)
				curMap.put(temps[j], MapUtil.getStringValue(formmap, temps[j] + "_" + i));
			i++;
			retList.add(curMap);
		}
		
		formmap.put(DynamicFormHelper.DYNTABLE_FORM_KEY, retList);
	}
	
	public static void dealDynTableBus(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, HashMap keyPairMap, String ret) {
		List subList = (List)dataMap.get(DynamicFormHelper.DYNTABLE_FORM_KEY);
		if(subList != null) {
			HashMap idmap = getSubIdMap(bussinessFacade, formDomain.getDynTableName(), formDomain.getDynTableParentid(), formDomain.getDynTableOrderBy(), ret);
			for(int j=0; j<subList.size(); j++) {
				HashMap subdataMap = (HashMap)subList.get(j);
				subdataMap.put("sequence", "" + (j + 1));
				MapUtil.copyMapValue(subdataMap, keyPairMap);
				String curid = MapUtil.getStringValue(subdataMap, "id");
				BussinessDealHelper.copyUserInfo(subdataMap, dataMap);
				if(StringUtil.isEmpty(curid)) {
					subdataMap.put(formDomain.getDynTableParentid(), ret);
					String parentkeys = formDomain.getDynTableParentkeys();
					if(!StringUtil.isEmpty(parentkeys)) {
						String[] temps = parentkeys.split("\\|");
						for(int i=0; i<temps.length; i++) {
							String[] temps2 = temps[i].split("~");
							subdataMap.put(temps2[0], MapUtil.getStringValue(dataMap, temps2[1]));
						}
					}
					subdataMap.put("districtcode", MapUtil.getStringValue(dataMap, "districtcode"));
					subdataMap.put("status", "0");
					bussinessFacade.save(formDomain.getDynTableName(), subdataMap);
				}else{
					subdataMap.put("status", "0");
					List queryList = new ArrayList();
					queryList.add(new QueryParamDomain("id", curid));
					idmap.remove(curid);
					HashMap curMap = bussinessFacade.getByID(formDomain.getDynTableName(), queryList);
					if(curMap.size() > 0)
						bussinessFacade.update(formDomain.getDynTableName(), subdataMap);
					else
						bussinessFacade.save(formDomain.getDynTableName(), subdataMap);
				}					
			}
			Iterator it = idmap.keySet().iterator();
			while(it.hasNext()) {
				String curid = it.next().toString();
				List queryList = new ArrayList();
				HashMap curMap = new HashMap();
				curMap.put("id", curid);
				curMap.put("status", "1");
				bussinessFacade.update(formDomain.getDynTableName(), curMap);
			}
		}
	}
	
	public static void dealQueryDynTableBus(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, HashMap keyPairMap, String ret) {
		List subList = (List)dataMap.get(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY);
		FormQueryDynTableDomain queryDynTableDomain = formDomain.getQueryDynTableDomain();
		if(subList != null) {
			for(int j=0; j<subList.size(); j++) {
				HashMap subdataMap = (HashMap)subList.get(j);
				subdataMap.put("sequence", "" + (j + 1));
				MapUtil.copyMapValue(subdataMap, keyPairMap);
				String curid = MapUtil.getStringValue(subdataMap, "id");
				BussinessDealHelper.copyUserInfo(subdataMap, dataMap);
				if(StringUtil.isEmpty(curid)) {
					subdataMap.put(queryDynTableDomain.getParentid(), ret);
					String parentkeys = queryDynTableDomain.getMapparentid();
					if(!StringUtil.isEmpty(parentkeys)) {
						String[] temps = parentkeys.split("\\|");
						for(int i=0; i<temps.length; i++) {
							String[] temps2 = temps[i].split("~");
							subdataMap.put(temps2[0], MapUtil.getStringValue(dataMap, temps2[1]));
						}
					}
					subdataMap.put("districtcode", MapUtil.getStringValue(dataMap, "districtcode"));
					subdataMap.put("status", "0");
					bussinessFacade.save(queryDynTableDomain.getMaptable(), subdataMap);
				}else{
					subdataMap.put("status", "0");
					List queryList = new ArrayList();
					queryList.add(new QueryParamDomain("id", curid));
					HashMap curMap = bussinessFacade.getByID(queryDynTableDomain.getMaptable(), queryList);
					if(curMap.size() > 0)
						bussinessFacade.update(queryDynTableDomain.getMaptable(), subdataMap);
					else
						bussinessFacade.save(queryDynTableDomain.getMaptable(), subdataMap);
				}					
			}
		}
	}
	
	public static void dealSaveDynTableBus(BussinessDaoFacade bussinessFacade, List subList, String tableName, HashMap dataMap, HashMap keyPairMap) {
		for(int j=0; j<subList.size(); j++) {
			HashMap subdataMap = (HashMap)subList.get(j);
			subdataMap.put("sequence", "" + (j + 1));
			MapUtil.copyMapValue(subdataMap, keyPairMap);
			subdataMap.put("credate", MapUtil.getStringValue(dataMap, "credate"));
			subdataMap.put("creator", MapUtil.getStringValue(dataMap, "creator"));
			subdataMap.put("modifydate", MapUtil.getStringValue(dataMap, "modifydate"));
			subdataMap.put("modifier", MapUtil.getStringValue(dataMap, "modifier"));
			subdataMap.put("status", "0");
			bussinessFacade.save(tableName, subdataMap);
		}
	}
	
	public static void dealDynTableBus(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, String ret) {
		dealDynTableBus(bussinessFacade, formDomain, dataMap, new HashMap(), ret);
	}
	
	public static void dealQueryDynTableBus(BussinessDaoFacade bussinessFacade, FormDomain formDomain, HashMap dataMap, String ret) {
		dealQueryDynTableBus(bussinessFacade, formDomain, dataMap, new HashMap(), ret);
	}
	
	private static HashMap getSubIdMap(BussinessDaoFacade bussinessFacade, String tableName, String column, String orderBy, String id) {
		HashMap retmap = new HashMap();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain(column, id));
		queryList.add(new QueryParamDomain("status", "0"));
		List valuelist = bussinessFacade.queryWithPostfix(tableName, queryList, orderBy);
		Iterator it = valuelist.iterator();
		while(it.hasNext()) {
			HashMap datamap = (HashMap)it.next();
			retmap.put(MapUtil.getStringValue(datamap, "id"), "");
		}
		
		return retmap;
	}
}
